#include "Particle.h"

namespace lib3dw
{
	Particle::Particle()
	{
		Particle(0.0f, 0.0f, 0.0f);
	}
	
	Particle::Particle(float posX, float posY, float posZ)
	{
		srand(time(0));
		Particle(true, 1.0f, (float)((rand() % 100) / 1000.0f + 0.003, 0.5f), 0.5f, 0.5f, 0.5f, posX, posY, posZ, ((float)(rand() % 50) - 26.0f) * 10.0f, ((float) (rand() % 50) - 25.0f) * 10.0f, ((float) (rand() % 50) - 25.0f) * 10.0f, 0.0f, -0.8f, 0.0f);
	}
	
	Particle::Particle(bool newActive, float newLife, float newFade, float newR, float newG, float newB, float newX, float newY, float newZ, float newXi, float newYi, float newZi, float newXg, float newYg, float newZg)
	{
		active = newActive;
		life = newLife;
		fade = newFade;
		r = newR;
		g = newG;
		b = newB;
		x = newX;
		y = newY;
		z = newZ;
		xi = newXi;
		yi = newYi;
		zi = newZi;
		xg = newXg;
		yg = newYg;
		zg = newZg;
	}
	
	Particle::~Particle()
	{
	}

	void Particle::Update(int slowdown)
	{
        x += xi / (slowdown * 1000);
		y += yi / (slowdown * 1000);
		z += zi / (slowdown * 1000);
		xi += xg;
		yi += yg;
		zi += zg;
		life -= fade;
		if(life < 0.0f)
		{
			life = 1.0f;
			fade = (float)(rand() % 100) / 1000.0f +  0.003f;
			x = 0.0f;
			y = 0.0f;
			z = 0.0f;
            xi = (float)(rand() % 60) - 32.0f;
			yi = (float)(rand() % 60) - 30.0f;
			zi = (float)(rand() % 60) - 30.0f;
			r = 0.5f;
			g = 0.5f;
			b = 0.5f;
         }
	}
	
	void Particle::Draw()
	{
         x = x;
         y = y;
         z = z;
         glColor4f(r, g, b, life);
         glBegin(GL_TRIANGLE_STRIP);
//         glTexCoord2d(1, 1);
         glVertex3f(x + 0.1f, y + 0.1f, z);
//         glTexCoord2d(0, 1);
         glVertex3f(x - 0.1f, y + 0.1f, z);
//         glTexCoord2d(1, 0);
         glVertex3f(x + 0.1f, y - 0.1f, z);
//         glTexCoord2d(0, 0);
         glVertex3f(x - 0.1f, y - 0.1f, z);
         glEnd();
	}
}